package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.constant.SystemPermissions;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.converter.VocChapterConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocChapterDO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocChapterDTO;
import com.cskaoyan.wordmemorize.request.*;
import com.cskaoyan.wordmemorize.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 顾海龙
 * @version 1.0
 */
@RestController
@ResponseBody
public class ChapterController {
    @Autowired
    ChapterService chapterService;

    @Autowired
    VocChapterConverter vocChapterConverter;

    //添加章节
    @PostMapping("/admin/voc/chapter")
    @SaAdminCheckPermission(SystemPermissions.Chapter.ADD)
    public Result AddChapter(@RequestBody VocChapterCommand command){
        try {
            VocChapterDO vocChapterDO = vocChapterConverter.vocChapterCommand2DO(command);
            chapterService.AddChapter(vocChapterDO);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }

        //返回异常待捕获
        return Result.ok();
    }

    //修改章节
    @PutMapping("/admin/voc/chapter")
    @SaAdminCheckPermission(SystemPermissions.Chapter.UPDATE)
    public Result UpdateChapter(@RequestBody VocChapterCommand command){
        try {
            VocChapterDO vocChapterDO = vocChapterConverter.vocChapterCommand2DO(command);
            chapterService.UpdateChapter(vocChapterDO);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //分页查询章节列表
    @GetMapping("/admin/voc/chapters/list")
    @SaAdminCheckPermission(SystemPermissions.Chapter.SEARCH)
    public Result selectChapterList(@ModelAttribute VocChapterPageRequest vocChapterPageRequest){
        PageDTO<VocChapterDTO> vocChapterDTOPageDTO = null;
        try {
            vocChapterDTOPageDTO = chapterService.selectChapterList(vocChapterPageRequest);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(vocChapterDTOPageDTO);
    }

    //批量删除章节
    @DeleteMapping("/admin/voc/chapters")
    @SaAdminCheckPermission(SystemPermissions.Chapter.BATCH_DELETE)
    public Result deleteChapterList(@RequestBody BatchDeleteCommand ids){
        try {
            chapterService.deleteChapterList(ids);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //删除指定章节
    @DeleteMapping("/admin/voc/chapter/{chapterId}")
    @SaAdminCheckPermission(SystemPermissions.Chapter.DELETE)
    public Result deleteChapter(@PathVariable Long chapterId){
        try {
            chapterService.deleteChapter(chapterId);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }
}
